package com.wlh.demo04;

import java.io.*;

public class Demo01 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //序列化：将对象写入到磁盘、网络IO流中
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:\\1.txt"));
        Person p = new Person(1, "小明", 18);
        oos.writeObject(p);
        oos.flush();
        oos.close();
        //反序列化：将磁盘中的对象数据源读出。
        //如果两次版本号不一致，则无法读出
        //Exception in thread "main" java.io.InvalidClassException: com.wlh.demo04.Person; local class incompatible: stream classdesc serialVersionUID = 1, local class serialVersionUID = 2
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\1.txt"));
        Person p1 = (Person) ois.readObject();
        System.out.println(p1.toString());
        ois.close();

    }
}
